﻿Imports Negocio
Imports Servicios
Imports Excepciones

Public Class frmConsultaTareas

    Private _proyecto As New Proyecto()

    Protected Overrides Sub PreLoad()
        Me.Icon = Icon.FromHandle(Global.SIPRO.My.Resources.Resources.task.GetHicon())
    End Sub

    Sub New(proyecto As Proyecto)
        InitializeComponent()

        _proyecto = proyecto
    End Sub
    Private Sub frmConsultaProyectos_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dtFecha.Value = Nothing

        Try
            cbUsuarios.FirstBlankItem = True
            cbUsuarios.DataSource = GesUsuarios.Consultar(Nothing, True)

            cbPrioridad.FirstBlankItem = True
            cbPrioridad.DataSource = System.Enum.GetValues(GetType(Nivel))

        Catch ex As Exception
            My.Application.HandlerException(ex)
            Me.Close()
        End Try
        Search()
    End Sub

    Sub Search()

        Try
            Dim usu As New Usuario()
            usu.OID = Nothing
            If TypeOf cbUsuarios.SelectedItem Is Usuario Then
                usu = DirectCast(cbUsuarios.SelectedItem, Usuario)
            End If

            Dim prioridad As Nivel = Nothing
            If TypeOf cbPrioridad.SelectedItem Is Nivel Then
                prioridad = DirectCast(cbPrioridad.SelectedItem, Nivel)
            End If

            dgvTareas.DataSource = GesTareas.Consultar(_proyecto, txtTitulo.Text, dtFecha.Value, usu, prioridad)

        Catch ex As Exception
            My.Application.HandlerException(ex)
            Me.Close()
        End Try
    End Sub

    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        Search()
    End Sub

    Private Sub btnCerrar_Click(sender As System.Object, e As System.EventArgs) Handles btnCerrar.Click
        Me.Close()
    End Sub

    Private Sub dgvTareas_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgvTareas.DoubleClick
        If dgvTareas.Rows.Count > 0 AndAlso TypeOf dgvTareas.CurrentRow.DataBoundItem Is Tarea Then
            Dim Tar As Tarea = DirectCast(dgvTareas.CurrentRow.DataBoundItem, Tarea)
            Dim f As New frmABMTarea(Tar, _proyecto)
            If f.ShowDialog = DialogResult.OK Then
                Search()
            End If
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If
    End Sub

    Private Sub btnNuevo_Click(sender As System.Object, e As System.EventArgs) Handles btnNuevo.Click
        Try
            Dim f = New frmABMTarea(_proyecto)
            If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Search()
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminar.Click
        If dgvTareas.Rows.Count > 0 AndAlso TypeOf dgvTareas.CurrentRow.DataBoundItem Is Tarea Then
            If MessageExt.Show(GesIdiomas.TraduceMensaje("MSGTAR001").ToString, MessageBoxButtons.OKCancel) = DialogResult.OK Then
                Dim Tar As Tarea = DirectCast(dgvTareas.CurrentRow.DataBoundItem, Tarea)
                Try
                    GesTareas.Eliminar(Tar)
                    Search()
                Catch ex As TareaActivityDeleteException
                    If MessageExt.Show(GesIdiomas.TraduceMensaje(ex.Code).ToString, MessageBoxButtons.OKCancel) = DialogResult.OK Then
                        GesTareas.Eliminar(Tar, False)
                        Search()
                    End If
                Catch ex As Exception
                    My.Application.HandlerException(ex)
                End Try
            End If
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If
    End Sub

    Private Sub btnRecursos_Click(sender As System.Object, e As System.EventArgs) Handles btnRecursos.Click
        If dgvTareas.Rows.Count > 0 AndAlso TypeOf dgvTareas.CurrentRow.DataBoundItem Is Tarea Then
            Dim Tar As Tarea = DirectCast(dgvTareas.CurrentRow.DataBoundItem, Tarea)
            Try
                Dim recursos As List(Of Recurso) = GesTareas.ObtenerRecursos(Tar)
                Dim f As New frmListaRecurso(recursos, Me.Tag)
                If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    GesTareas.ActualizarRecursos(Tar, recursos)
                End If
            Catch ex As Exception
                My.Application.HandlerException(ex)
            End Try
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If
    End Sub

    Private Sub btnActividades_Click(sender As System.Object, e As System.EventArgs) Handles btnActividades.Click

        If dgvTareas.Rows.Count > 0 AndAlso TypeOf dgvTareas.CurrentRow.DataBoundItem Is Tarea Then
            Dim Tar As Tarea = DirectCast(dgvTareas.CurrentRow.DataBoundItem, Tarea)
            Try
                Dim f As New frmConsultaActividades(Tar)
                f.ShowDialog()
                'Search()
            Catch ex As Exception
                My.Application.HandlerException(ex)
            End Try
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If

    End Sub

    Private Sub btnReporte_Click(sender As System.Object, e As System.EventArgs) Handles btnReporte.Click
        If dgvTareas.Rows.Count > 0 AndAlso TypeOf dgvTareas.CurrentRow.DataBoundItem Is Tarea Then
            Dim Tar As Tarea = DirectCast(dgvTareas.CurrentRow.DataBoundItem, Tarea)
            Try
                Dim f As New frmReportTask(Tar)
                f.Show()
            Catch ex As Exception
                My.Application.HandlerException(ex)
            End Try
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If
    End Sub
End Class